package com.jsnkj.basic.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.jsnkj.basic.R
import com.jsnkj.basic.config.SpConfig
import com.jsnkj.basic.databinding.ActivityRegimentalRecruitmentBinding
import com.jsnkj.basic.dialog.RegimentalRecruitmentDialog
import com.jsnkj.basic.mvvm.BaseKtActivity
import com.jsnkj.basic.util.MediaUtil
import com.jsnkj.basic.util.QRCodeUtil
import com.bumptech.glide.Glide
import com.jsnkj.basic.LibraryConfig


//团长招募
class RegimentalRecruitmentActivity :
    BaseKtActivity<ActivityRegimentalRecruitmentBinding, InviteFriendsViewModel>(),
    View.OnClickListener {

    companion object {
        fun startRecruitmentActivity(activity: Activity, recruitmentJson: String) {
            val intent = Intent(activity, RegimentalRecruitmentActivity::class.java)
            intent.putExtra("recruitment_json", recruitmentJson)
            activity.startActivity(intent)
        }
    }

    override val layoutId: Int
        get() = R.layout.activity_regimental_recruitment


    var recruitmentJson: String = ""
    var bitmapQR: Bitmap? = null

    var recruitmentDialog: RegimentalRecruitmentDialog? = null

    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this

        recruitmentJson = intent.getStringExtra("recruitment_json") ?: ""
        if (recruitmentJson.isEmpty()) {
            com.jsnkj.basic.util.ToastUtils.show("获取招募二维码失败")
            onBackPressed()
            return
        }

        //是否启用成为团长功能
        if (LibraryConfig.isEnableBecomeColonel()) {
            mVDB.txtGoHead.visibility = View.VISIBLE
        } else {
            mVDB.txtGoHead.visibility = View.GONE
        }

        bitmapQR = QRCodeUtil.createQRCodeBitmap(
            recruitmentJson, 100, 100,
            "UTF-8", "H", "1", Color.BLACK, Color.WHITE
        )

        Glide.with(this@RegimentalRecruitmentActivity).load(bitmapQR).into(mVDB.ivQr)
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.txt_regimental_recruitment -> {

                if (recruitmentDialog?.isShowing == true) {
                    recruitmentDialog?.dismiss()
                }

                recruitmentDialog =
                    RegimentalRecruitmentDialog(this@RegimentalRecruitmentActivity, recruitmentJson,
                        object : RegimentalRecruitmentDialog.onItemClickListener {
                            override fun saveItemClick(bitmap: Bitmap?) {
                                if (null == bitmap) {
                                    com.jsnkj.basic.util.ToastUtils.show("保存失败")
                                    return
                                }
                                MediaUtil.savePuzzleBitmap(
                                    this@RegimentalRecruitmentActivity,
                                    bitmap,
                                    SpConfig.IMG_SAVE_NAME
                                ) { state, _ ->
                                    if (state) {
                                        bitmap.recycle()
                                        com.jsnkj.basic.util.ToastUtils.show("已成功保存到相册")
                                    } else {
                                        com.jsnkj.basic.util.ToastUtils.show("保存失败，请重试")
                                    }
                                }
                            }
                        })

                recruitmentDialog?.show()
            }

            R.id.image_return -> {
                onBackPressed()
            }

            R.id.txt_go_head -> {
                startActivity(GoHeadActivity::class.java)
            }
        }
    }


}